package com.atguigu.cmsservice.controller;


import com.atguigu.cmsservice.entity.CrmBanner;
import com.atguigu.cmsservice.service.CrmBannerService;
import com.atguigu.commonutils.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author Return
 * @since 2021-04-01
 */
@RestController
@RequestMapping("/cmsservice/banner")
@CrossOrigin
public class CrmBannerController {

    @Autowired
    CrmBannerService crmBannerService;

    @ApiOperation(value = "获取Banner")
    @GetMapping("{id}")
    public R getById(@PathVariable String id){
        CrmBanner byId = crmBannerService.getById(id);
        return R.ok().data("item",byId);
    }

    @ApiOperation(value = "删除Banner")
    @DeleteMapping("{id}")
    public R delete(@PathVariable String id){
        boolean b = crmBannerService.removeById(id);
        if(b){
            return R.ok().message("删除成功！");
        }
        return R.error().message("删除失败！");

    }

    @ApiOperation(value = "修改banner")
    @PutMapping("{id}")
    public R modifyBanner(@PathVariable String id,
                       @RequestBody CrmBanner crmbanner){
        boolean b = crmBannerService.updateById(crmbanner);
        if (b){
            return R.ok().message("更新成功！！");
        }
        return R.error().message("更新失败！！");

    }

    @ApiOperation(value = "添加banner")
    @PostMapping
    public R addBanner(@RequestBody CrmBanner crmBanner){
        boolean save = crmBannerService.save(crmBanner);
        if (save){
            return R.ok().message("添加成功！！");
        }
        return R.error().message("添加失败！！");
    }

    @ApiOperation(value = "获取分页查询数据")
    @GetMapping("{page}/{limit}")
    public R getList(@PathVariable Long page,
                     @PathVariable Long limit){
        Page<CrmBanner> crmBannerPage = new Page<>(page,limit);
        crmBannerService.page(crmBannerPage,null);
        List<CrmBanner> records = crmBannerPage.getRecords();
        return R.ok().data("items",records).data("total",crmBannerPage.getTotal());
    }

}

